#!/usr/bin/python
# -*- coding: UTF-8 -*-

"""
@author: jiu
@contact: 643562684@qq.com
@time: 2022/3/10 14:22
"""

from typing import Union, Type
from common.base_read_file import BASE_FILE
from Conf.setting import *


# from common.log import logger   来回相互调用会报错


class Read_InI(BASE_FILE):
    def __init__(self, ini_file: str = PROJECT_CONF_SETTING['project'],
                 section: str = 'mysql'):

        super(Read_InI, self).__init__(ini_file)
        self.section = section
        self.cf.read(self._file)

    @property
    def sections(self):
        """
            [MYSQL]
            ......

            [REDIS]
            ...
        :return: ['MYSQL','REDIS']
        """
        return self.cf.sections()

    @property
    def data(self):
        if not self._data:
            try:
                for sec in self.sections:
                    self._data[sec] = self.items(sec)
                return self._data
            except Exception as e:
                raise e
        return self._data

    def options(self, sec: str):
        """
               [MYSQL]
        :param sec:  MYSQL -> sec
        :return: all keys  [host,port,username,password] only key is not dict
        """
        return self.cf.options(sec)

    def items(self, sec: str):
        """
        [('host','localhost'),('port','3306')....]
        :param sec: MYSQL -> sec
        :return: type dict([int.])
        """
        data = dict(self.cf.items(sec))
        for key in data:
            if key in CONVER_LIST:
                data[key] = int(data[key])

        return data

    def get_option_data(self, section: str = '', name: str = '', return_type=None):
        """获取指定的分组表的"""
        return eval(return_type)(self.cf.get(section, name))


